<?php
/**
* Filename : HTML5Element.php
*
* License: GNU GPL V3
*
* Author: Marquish Doranga
* Date Created: 23rd Dec, 2012
* Last Modified: 23rd Dec, 2012
* Version : 1.0
*
* Targetd Audience: PHP OOP Developers, Programming Enthusiasts.
* 
*/
/**
* Purpose :
* The class HTML5Element helps to create structured html5 elements and options 
* Object Oriented Feature of PHP applied for extensibility and usage.
*
* Usage: see Test() function at the end.
*/
final class HTML5Element{
	private $options;
	private $content=null;
	private $elementobject=null;
	function __construct($name){ $this->elementobject=$name; }
	private function add_option($param,$val){  //remember this property of php. 
		$this->options[]=array($param=>$val);
		
	}
	private function add_content($content){ // add text.
		$this->content[]=$content;
	}
	function __toString(){
		$str = '<'.$this->elementobject; // start element
		try{
		if($this->options==null) throw new Exception('No Options Applied to Element');
		foreach($this->options as $s){
			foreach($s as $opt=>$val){
				$str.= ' '.$opt.'="'.$val.'"'; // populate all options of elements 
			}
		}
		}catch(Exception $e){}
		
		if($this->content==null){ // is closed element ie <img .. />
			$str.='/>';
		}
		else{ // should be completed element like <aside.. >..</aside>
			$str.='>';
			foreach($this->content as $cont){
					$str.=$cont; // populate every contents.
			}
			$str.="</{$this->elementobject}>"; // close here.
		}
		return $str; // return element.
	}
	
	function __call($name, $args){ // arg passed as option, val as value
		if($name=="content"){ // if the argument is 'content' then pass as content
			return call_user_func_array(array($this, add_content),$args);
		}
		else{ //else create option parameters and add
			//$val = array($name=>$args[0]); // array formation
			//print_r($val);
			return call_user_func_array(array($this, add_option),array($name,$args[0])); 
			
		}
	}
}

function test(){
$k = new HTML5Element('article');
$k->onclick("Javascript:alert('hi there');");
$k->class('myclass');
$k->id('myid');
$k->content('Welcome to world');

$e = new HTML5Element('body');
//$e->script("javascript:alert('hello')");
$e->content($k);

$hd = new HTML5Element('title');
$hd->content("HTML5 with OO PHP:- Sample by marquish doranga");
$hde = new HTML5Element('head');
$hde->content($hd);
echo $hde;
echo $e;
}
 test();
?>